<template>
	<div class="partnerList">
		<div class="partnerList_top">
			<el-breadcrumb separator-class="el-icon-arrow-right" style="margin-top: 10px;">
				<el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
				<el-breadcrumb-item :to="{ path: '/teamList' }">产品包商家授权</el-breadcrumb-item>
				<el-breadcrumb-item>商家列表</el-breadcrumb-item>
			</el-breadcrumb>
		</div>
		<!-- 头部导航 -->
		<div class="navTab">
			<div class="navTabList">
				<div class="navTabItem" :class="{ navTabItem_active: navIndex == index }"
					v-for="(item, index) in navTab" :key="index" @click="navFn($event)" :data-index="index">
					{{ item }}
					<div class="navBorder" v-if="navIndex == index"></div>
				</div>
			</div>
		</div>
		<div class="partnerList_bottom">
			<div class="partnerList_bottom_new">
				<div class="partnerList_bottom_new_goods">
					<div class="partnerList_bottom_new_goods_screening">
						<div class="item">
							<el-button type="primary" size="medium" style="margin-right: 20px" @click="addMerchant">
								新增授权商家
							</el-button>
						</div>
						<span>卖场：</span>
						<el-select v-model="marketId" clearable @change="selectMarket" filterable placeholder="请选择卖场">
							<el-option v-for="item in marketList" :key="item.id" :label="item.name" :value="item.id">
							</el-option>
						</el-select>
						<div class="item">
							<el-input v-model="keyword" placeholder="搜索" clearable></el-input>
						</div>
						<div class="item">
							<button @click="handerSeach" class="searchBtn"><i class="el-icon-search"></i>搜索</button>
						</div>
					</div>
				</div>
			</div>
			<div class="partnerList_table">
				<div class="partnerList_bottom_table">
					<table>
						<tr>
							<th></th>
							<th>序号</th>
							<th>店铺名称</th>
							<th>手机号</th>
							<th>logo</th>
							<th>商家类型</th>
							<th>所属卖场</th>
							<th>创建时间</th>
							<th>操作</th>
						</tr>
						<tr v-for="(v,i) in listData" :key="i">
							<td>
								<font class="checkbox" :class="{'check_active' : checkBoxAll.includes(i)}"
									@click="handlerCheckbox(i)"><i class="el-icon-check"></i></font>
							</td>
							<td>{{v.id}}</td>
							<td>{{v.store_name}}</td>
							<td>{{v.store_mobile}}</td>
							<td>
								<div style="width: 80px;height: 80px;overflow: hidden;margin:4px auto;">
									<img :src="v.store_logo" style="width: 100%;height: 100%;">
								</div>
							</td>
							<td>
								{{storeListType[v.type]}}
							</td>
							<td>
								<template v-if="v.market_name">
									{{v.market_name}}
								</template>
								<template v-else>
									---
								</template>
							</td>
							<td>
								{{v.created_at}}
							</td>
							<td>
								<button class="editor delete" @click="cancelAuth(v.store_id)">解绑商户</button>
							</td>
						</tr>
						<tr v-if="listData.length == 0">
							<td class="table_title" colspan="10">当前数据空空如也</td>
						</tr>
					</table>
					<div class="partnerList_bottom_page">
						<!-- <div class="partnerList_bottom_page_left">
							<div class="partnerList_bottom_page_left_checkAll" @click="handerCheckAll">
								<font class="checkbox" :class="{'check_active' : checkAll}"><i
										class="el-icon-check"></i></font>
							</div>全选
							<button class="partnerList_bottom_page_left_btn" @click="selectDel">删除</button>
						</div> -->
						<div class="partnerList_bottom_page_left"></div>
						<div class="partnerList_bottom_page_right">
							<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
								:current-page="page" :page-sizes="[10, 20, 30, 40]" :page-size="pageSize"
								layout="total, sizes, prev, pager, next, jumper" :total="total" background>
							</el-pagination>
						</div>
					</div>
				</div>
			</div>
			<!-- 添加商户 -->
			<modal :title="MadalTitle" v-model="merchantShow" :centered="true" :maskClosable="false" @ok="addStore"
				@cancel="storeCancel">
				<div>
					<el-form ref="ruleForm" label-width="100px" class="demo-ruleForm">
						<el-form-item label="商户类型" prop="name">
							<el-select v-model="type" filterable placeholder="请选择" @change="selectStoreType">
								<el-option v-for="item in storeType" :key="item.type" :label="item.store_name"
									:value="item.type">
								</el-option>
							</el-select>
						</el-form-item>
						<el-form-item label="选择商户" prop="name">
							<el-select v-model="store_id" filterable placeholder="请选择">
								<el-option v-for="item in storeList" :key="item.id" :label="item.store_name"
									:value="item.id">
								</el-option>
							</el-select>
						</el-form-item>
					</el-form>
				</div>
			</modal>
		</div>
	</div>
</template>

<script>
	import modal from 'ant-design-vue/lib/modal';
	import 'ant-design-vue/lib/table/style/css'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
	import 'ant-design-vue/lib/modal/style/css'; // 或者 ant-design-vue/lib/button/style/css 加载 css 文件
	export default {
		components: {
			modal
		},
		data() {
			return {
				listData: [],
				keyword: '',
				cate: {},
				input: '',
				navIndex: 0,
				checkAll: false,
				page: 1,
				pageSize: 10,
				total: 0,
				checkBoxAll: [], // 选中的数据
				shelvesAlls: [],
				tableList: [],
				status: 1, //团队状态
				visibleShow: false,
				merchantShow: false, //编辑窗口显示
				dialogVisible: false,
				dialogImageUrl: "", //图片
				fileList: [],
				statusList: [{
					value: 1,
					label: '正常'
				}, {
					value: 2,
					label: '隐藏'
				}],
				MadalTitle: '',
				market_id: '', //卖场
				activityList: [], //活动列表
				activityKeyword: '', //活动搜索
				storeList: [], //商户列表
				store_id: '', //选择的商户
				edit: false, //是否编辑
				marketList: [],
				marketId: 1, //选择搜索的卖场id
				type: 'shop', //商户类型
				storeType: [{
						store_name: '商家',
						type: 'shop'
					},
					{
						store_name: '装企',
						type: 'decoration'
					},
					{
						store_name: '设计师',
						type: 'designer_per'
					},
					{
						store_name: '施工队',
						type: 'builder'
					},
				],
				navTab: ["商家", "装企", "设计师", "施工队"],
				storeListType: {
					shop: '商家',
					decoration: '装企',
					builder: '施工队',
					designer_per: '设计师'
				}
			}
		},
		mounted(options) {
			//let room_id = this.$route.query.room_id
			//团队列表
			this.getListAll();
			//卖场
			this.remoteMethod();
		},

		methods: {
			// 获取卖场列表
			remoteMethod() {
				this.$http.post("/api/root/Market/marketList").then((res) => {
					if (res.code == 1) {
						this.marketList = res.data ? res.data : [];
					}
				});
			},
			// 选择卖场
			selectMarket() {
			 this.getListAll();
			},
			//选择商户类型
			selectStoreType() {
				this.getStoreList();
			},
			//解绑商户
			cancelAuth(store_id) {
				let params = {
					type:this.type,
					store_id: store_id,
				}
				this.$confirm('是否确定解绑', '提示').then(() => {
					this.$http.post('/api/root/newpk/auth/cancelAuth', params, {}).then(res => {
						if (res.code == 1) {
							this.$message.success('解绑成功')
							this.getListAll()
							this.page = 1;
						} else {
							this.$message.warning(res.msg)
						}
					}).catch(err => {
						this.$message.warning('请求异常')
					})
				}).catch(() => {

				});
			},
			handleAdd() {
				this.MadalTitle = "新增授权商家";
				this.visibleShow = true;
			},
			//获取商家授权列表
			getListAll() {
				let params = {
					keyword: this.keyword,
					page: this.page,
					pagesize: this.pageSize,
					market_id: this.marketId,
					type: this.type,
				}
				let that = this;
				that.$http.post('/api/root/newpk/auth/store', params).then(res => {
					if (res.code === 1) {
						var data = res.data.stores
						if (data.length > 0) {
							that.total = res.data.total
							that.listData = data
						} else {
							that.total = 0
							that.listData = []
						}
					} else {
						that.$message.error(res.msg)
					}
				}).catch(err => {
					that.$message.error(err.msg)
				})
			},
			// 图片上传
			handleRemove(file, fileList) {
				this.fileList = fileList;
			},
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;

				this.dialogVisible = true;
			},
			handleExceed(files, fileList) {
				this.$message.warning(`当前限制上传一张图片,请先删除原图片`);
			},
			handleAvatarSuccess(res, file, fileList) {
				this.fileList = fileList;
			},
			beforeAvatarUpload(file) {
				const isLt2M = file.size / 1024 / 1024 < 2;
				if (!isLt2M) {
					this.$message.error("上传商品图片大小不能超过 2MB!");
				}
				return isLt2M;
			},
			getHMS(time) {
				const hour = parseInt(time / 3600) < 10 ? '0' + parseInt(time / 3600) : parseInt(time / 3600)
				const min = parseInt(time % 3600 / 60) < 10 ? '0' + parseInt(time % 3600 / 60) : parseInt(time % 3600 / 60)
				const sec = parseInt(time % 3600 % 60) < 10 ? '0' + parseInt(time % 3600 % 60) : parseInt(time % 3600 % 60)
				return hour + ':' + min + ':' + sec
			},
			// 点击头部nav
			navFn(e) {
			  this.page = 1
			  this.navIndex = e.target.dataset.index;
			  this.type=this.storeType[e.target.dataset.index].type;
			  this.getListAll();
			},
			// 编辑详情
			toEdit(id, activity_id) {
				this.MadalTitle = "新增授权商家"
				this.visibleShow = true
				this.activity_id = activity_id;
				this.edit = true;
				this.team_id = id;
				let params = {
					id: id
				}
				let that = this;
				that.$http.post('/api/root/activity/team/detail', params).then(res => {
					if (res.code === 1) {
						let data = res.data
						this.market_id = data.market_id;
						this.activity_id = data.activity_id;
						this.title = data.title;
						this.sub_title = data.sub_title;
						this.status = data.status;
						if (data.icon != '') {
							this.fileList.push({
								url: data.icon,
								id: ''
							});
						}
					} else {
						that.$message.error(res.msg)
					}
				})

			},
			//添加商户
			addMerchant(id, activity_id) {
				this.MadalTitle = "添加商户"
				this.merchantShow = true
				this.activity_id = activity_id;
				this.getStoreList();
			},
			//获取商户列表
			getStoreList() {
				let params = {
					type: this.type,
					keyword: this.keyword,
				}
				let that = this;
				that.$http.post('/api/root/newpk/auth/search', params).then(res => {
					if (res.code === 1) {
						let data = res.data
						if (data.length > 0 || res.code === 1) {
							that.storeList = data

						} else {
							that.storeList = []
						}
					} else {
						that.$message.error(res.msg)
					}
				}).catch(err => {
					that.$message.error(err.msg ? err.msg : err.toString())
				})
			},
			//添加团队商户
			addStore() {
				let params = {
					store_id: this.store_id,
					type: this.type,
				}
				let that = this;
				that.$http.post('/api/root/newpk/auth/toAuth', params).then(res => {
					if (res.code === 1) {
						that.$message.success('添加成功')
						this.getListAll()
						this.page = 1;
					} else {
						that.$message.error(res.msg)
					}
				}).catch(err => {
					that.$message.error(err.msg ? err.msg : err.toString())
				})
			},
			handleCancel() {
				this.MadalTitle = ''
				this.dialogVisible = false
				this.visibleShow = false
				this.fileList = [];
			},
			//隐藏弹窗
			storeCancel() {
				this.MadalTitle = ''
				this.merchantShow = false
			},

			// 选择状态
			selectStatus() {
				this.page = 1
				this.getShareList(this.room_id)
			},
			// 多选
			handlerCheckbox(i) {
				console.log(i);
				let arr = this.checkBoxAll;
				if (arr.includes(i)) {
					let index = arr.indexOf(i);
					if (index != -1) {
						arr.splice(index, 1);
						this.shelvesAlls.splice(index, 1);
						if (this.checkBoxAll.length !== this.tableList.length) {
							this.checkAll = false;
						}
					}
				} else {
					this.checkBoxAll.push(i);
					this.shelvesAlls.push(this.tableList[i].id);
					if (this.checkBoxAll.length == this.tableList.length) {
						this.checkAll = true;
					}
				};
				console.log(this.shelvesAlls);
			},
			// 全选与全不选
			handerCheckAll() {
				this.checkAll = !this.checkAll;
				let arr = this.tableList.length;
				this.checkBoxAll = [];
				this.shelvesAlls = [];
				if (this.checkAll) {
					for (let i = 0; i < arr; i++) {
						this.checkBoxAll.push(i);
					}
					this.checkBoxAll.map(v => {
						this.shelvesAlls.push(this.tableList[v].id.toString());;
					})
				} else {
					this.checkBoxAll = [];
					this.shelvesAlls = []
				}
				console.log(this.shelvesAlls);
			},
			//搜索
			handerSeach() {
				if (!this.marketId) {
					this.$message.error('请先选择卖场')
					return;
				}
				this.getListAll()
			},

			handleSizeChange(val) {
				this.pageSize = val;
				this.getListAll()
			},
			handleCurrentChange(val) {
				this.page = val;
				this.getListAll()
			},
			formatDate(value) {
				let date = new Date(parseInt(value) * 1000);
				let y = date.getFullYear();
				let m = date.getMonth() + 1;
				m = m < 10 ? "0" + m : m;
				let d = date.getDate();
				d = d < 10 ? "0" + d : d;
				let h = date.getHours();
				h = h < 10 ? "0" + h : h;
				let minute = date.getMinutes();
				let second = date.getSeconds();
				minute = minute < 10 ? "0" + minute : minute;
				second = second < 10 ? "0" + second : second;
				return y + "-" + m + "-" + d + " " + h + ":" + minute + ":" + second;
			},
			//商户列表跳转
			pageStore(id, market_id) {
				this.$router.push({
					name: "teamStoreList",
					query: {
						id,
						market_id
					},
				});
			}
		}
	}
</script>

<style lang="less" scoped>
	@import 'index.less';

	// 头部导航
	.navTab {
		width: 100%;
		height: 140px;
		padding: 20px 20px;
		background-color: #f5f5f5;

		.navTabList {
			position: relative;
			width: 100%;
			height: 120px;
			background-color: #fff;
			display: flex;
			padding: 20px 0;
			text-align: center;

			// line-height: 80px;
			.navTabItem {
				font-size: 16px;
				display: flex;
				flex-direction: column;
				padding: 0 15px;
				margin: 0 20px;
				line-height: 50px;
				align-items: center;
				cursor: pointer;

				.navBorder {
					width: 25px;
					height: 5px;
					background-color: #409EFF;
				}
			}

			.navTabItem_active {
				color: #409EFF;
			}
		}

		.navTabList::before {
			content: '';
			width: 100%;
			position: absolute;
			left: 0px;
			bottom: 35px;
			border-bottom: 2px solid rgba(0, 0, 0, 0.3);
		}

	}

	.partnerList_bottom_new_goods_screening {
		width: 100%;
		height: auto;
		display: flex;

		.searchBtn {
			max-width: 90px;
			min-width: 68px;
			height: 42px;
			line-height: 42px;
			background-color: #409EFF;
			margin-left: 5px;
			border: 0 none;
			outline: none;
			border-radius: 4px;
			color: #fff;
			cursor: pointer;
		}

		.inputBox {
			margin-right: 20px;
		}
	}
</style>
<style>
	.ant-modal {
		width: 590px !important;
	}
</style>
